package com.gsx.Controller;

import com.github.pagehelper.PageInfo;
import com.gsx.entity.Blog;
import com.gsx.entity.UserInfo;
import com.gsx.service.BlogService;
import com.gsx.service.TypeService;
import com.gsx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class SelfController {
    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/selfmessage")
    public String selfMessage(HttpSession session, Model model,RedirectAttributes attributes){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        if(userInfo==null){
            attributes.addFlashAttribute("message","请先登录");
            return "redirect:/log";
        }
        model.addAttribute("blogCount",userService.getUserBlogCont(userInfo.getUser_id()));
        return "selfSpace";
    }


    @GetMapping("/selfblog")
    public String selfBlog(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                           @RequestParam(name="size",required = true,defaultValue = "5")int size, HttpSession session
    , Model model, RedirectAttributes attributes){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        if(userInfo==null){
            attributes.addFlashAttribute("message","请先登录");
            return "redirect:/log";
        }
        PageInfo pageInfo=new PageInfo(blogService.getSelfBlog(userInfo.getUser_id(),page,size));
        model.addAttribute("pageInfo",pageInfo);
        return "blogControl";
    }

    @GetMapping("selfblogs/{id}/delete")
    public String delBlog(RedirectAttributes attributes,@PathVariable Long id){
        int result=blogService.deleteBlog(id);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/selfblog";
    }

    @PostMapping("/blogUpdate")
    public String blogUpdate(UserInfo userInfo,HttpSession session,RedirectAttributes attributes){
        UserInfo user=(UserInfo) session.getAttribute("user");
        userInfo.setUser_id(user.getUser_id());
        int result=userService.updateUser(userInfo);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
            return "redirect:/selfmessage";
        }else{
            attributes.addFlashAttribute("message","操作成功");
            session.setAttribute("user",userService.getUserInfo(user.getUsername()));
            return "redirect:/selfmessage";
        }
    }

    /*写博客*/
    @GetMapping("/bloginput")
    public String blogInput(Model model){
        model.addAttribute("types",typeService.getAll());
        model.addAttribute("blog",new Blog());
        return "blogs-input";
    }

    /*新增*/
    @PostMapping("/blogs")
    public String post(Blog blog,RedirectAttributes attributes, HttpSession session){
        UserInfo user=(UserInfo)session.getAttribute("user");
        blog.setUser_id(user.getUser_id());
        int result=blogService.saveBlog(blog);

        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/selfblog";
    }
    /*修改*/
    @PostMapping("/blogs/{id}")
    public String editInput(RedirectAttributes attributes, @PathVariable Long id, Blog newBlog){
        Blog blog=blogService.getBlog(id);
        newBlog.setBlog_id(id);
        newBlog.setCreateTime(blog.getCreateTime());
        newBlog.setViews(blog.getViews());
        newBlog.setUpdateTime(new Date());
        newBlog.setUser_id(blog.getUser_id());
        int result=blogService.updateBlog(newBlog);

        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/selfblog";
    }

    /*修改*/
    @GetMapping("/blogs/{id}/input")
    public String edit(Model model,@PathVariable Long id){
        model.addAttribute("types",typeService.getAll());
        model.addAttribute("blog",blogService.getBlog(id));
        model.addAttribute("blog_id",id);
        return "blogs-input";
    }
}
